package main.menu;
import java.io.FileWriter;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;

import main.staff.CreateTypesSql;
import main.staff.MenuAndAuth;
import main.util.CellRangeAddressUtil;
import main.util.CreateAnySql;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;


public class CreateMenuSql {

	public FileWriter module,menu,authelement,delete;
	
	public Sheet sheet;
	
	//判断合并单元格工具类
	public CellRangeAddressUtil cau;
	
	public Integer DISPLAYNO=22;
	
	public String methodName="getMenuSql";

	public void setDISPLAYNO(Integer dISPLAYNO) {
		DISPLAYNO = dISPLAYNO;
	}

	public void setDelete(FileWriter delete) {
		this.delete = delete;
	}




	public CreateMenuSql(FileWriter module, FileWriter menu,FileWriter authelement, Sheet sheet, String methodName) {
		super();
		this.module = module;
		this.menu = menu;
		this.authelement = authelement;
		this.sheet = sheet;
		this.methodName = methodName;
		
		cau=new CellRangeAddressUtil(sheet);
	}


	//flag不为null时表示只获取按钮sql
	public void operateSheet(String flag){
		try {
			for(int i=0;i<sheet.getLastRowNum()+1;i++){
				Row row = sheet.getRow(i);
				String moduleId=CreateTypesSql.removeNull(row, 0);
				if(moduleId.indexOf("-")<0){
					System.out.println(i+"、"+moduleId+"失败");
					continue;
				}
				
				String[] names=moduleId.split("-");
				//写入模块
				module.write(CreateAnySql.getModuleSql(names,DISPLAYNO));
				
				LinkedHashMap<String, String> map=new LinkedHashMap<String, String>();
				map.put("MODULEID", names[1]);
				map.put("SUPERCODE", "000");
				map.put("AUTHID", "000"+names[1]);
				map.put("DISPLAYNO", DISPLAYNO.toString());
				++DISPLAYNO;
				
				//删除相关
				delete.write("delete from t_ucp_menu where moduleid='"+names[1]+"';\n");
				delete.write("delete from t_ucp_authelement where moduleid='"+names[1]+"';\n");
				
				System.out.println(i+"、.....................working");
				
				if(flag==null){
					i=writeMenu(0, row, map);
				}else{
					i=writeButton(0, row, map);
				}
				System.out.println(".......................end work");
			}
		} catch (Exception e) {
			System.out.println("'''''''''''''''''''''''''sheet页："+sheet.getSheetName()+" 添加失败");
			e.printStackTrace();
		}
	}
	
	
	
	//固定样式的生成菜单和按钮信息
	public int writeMenu(int col,Row row,LinkedHashMap<String, String> map) throws Exception{
		Integer endRow;
		if(col>3){
			return row.getRowNum();
		}
		
		MenuAndAuth maa=new MenuAndAuth(col, row,map);
		if("".equals(map.get("MENUNAME"))){
			System.out.println("!!!!"+col+"内容为nul、添加无效，暂不添加  !!!!");
			return row.getRowNum();
		}
		
		//生成auth 的 sql
		authelement.write(maa.getAuthSql());
		//生成menu的sql
		Method me=MenuAndAuth.class.getMethod(methodName);//需要根据crm系统生成
		String menuSql=(String)me.invoke(maa);
		menu.write(menuSql);
		
		//如果是按钮需要生产mo值
		if(col==3){
			row.getCell(col+1).setCellValue(map.get("AUTHID"));
		}
		
		this.logLook(map);
		
		
		endRow=cau.getMergeRow(row.getCell(col));
		
		if(endRow!=null){
			int j=1;
			for(int i=row.getRowNum();i<endRow+1;i++){
				int nextCol=col+1;
				LinkedHashMap<String, String> nextMap=maa.createNext(j);
				i=writeMenu(nextCol, sheet.getRow(i), nextMap);
				++j;
			}
			
			return endRow;
		}
		
		LinkedHashMap<String, String> nextMap=maa.createNext(1);
		writeMenu(col+1, row, nextMap);
		
		return row.getRowNum();
	}
	
	//固定样式的生成菜单和按钮信息
	public int writeButton(int col, Row row, LinkedHashMap<String, String> map)	throws Exception {
		Integer endRow;
		if (col > 3) {
			return row.getRowNum();
		}

		MenuAndAuth maa = new MenuAndAuth(col, row, map);
		maa.setButtonAuth();
		if ("".equals(map.get("MENUNAME"))) {
			System.out.println("!!!!" + col + "内容为nul、添加无效，暂不添加  !!!!");
			return row.getRowNum();
		}

		// 生成auth 的 sql
		authelement.write(maa.getButtonAuthSql());

		// 如果是按钮需要生产mo值
		if (col == 3) {
			row.getCell(col + 1).setCellValue(map.get("AUTHID"));
		}

		this.logLook(map);

		endRow = cau.getMergeRow(row.getCell(col));

		if (endRow != null) {
			int j = 1;
			for (int i = row.getRowNum(); i < endRow + 1; i++) {
				int nextCol = col + 1;
				LinkedHashMap<String, String> nextMap = maa.createNext(j);
				i = writeButton(nextCol, sheet.getRow(i), nextMap);
				++j;
			}

			return endRow;
		}

		LinkedHashMap<String, String> nextMap = maa.createNext(1);
		writeButton(col + 1, row, nextMap);

		return row.getRowNum();
	}
	
	public void setMO(){
		
	}
	
	public void logLook(LinkedHashMap<String, String> map){
		String menuId=map.get("MENUID");
		String menuName=map.get("MENUNAME");
		if(menuId!=null){
			Integer suffix=new Integer(menuId.length());
			for(int i=0;i<suffix;i++){
				System.out.print("**");
			}
			System.out.println(menuId+"="+menuName);
		}
	}
}
